﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.User
{
    public partial class AddRole : Base.BasePage
    {
        LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
        string userNameTemp;
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("YHGL");
            strPageUserType.Add("mana");
            userNameTemp = Server.UrlDecode(Request.QueryString["UserName"].ToString());
            if (!IsPostBack)
            {
                if (Request.QueryString["UserID"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "你无权查看此页面！");
                }
                else
                {
                    int UserID = int.Parse(Request.QueryString["UserID"].ToString());
                    InitBind(UserID);
                }
            }
        }
        protected string userName
        {
            get
            {
                return userNameTemp;
            }
        }


        /// <summary>
        /// 页面初始化(已经添加的角色不显示)
        /// </summary>
        protected void InitBind(int UserID)
        {
            ExtendBLL.UserRole EBUR = new ExtendBLL.UserRole();
            gvRole.DataSource = EBUR.GetRoleTableExpect(UserID);
            gvRole.DataBind();
        }

        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddRoleForUser_Click(object sender, EventArgs e)
        {
            LabMS.BLL.UserRole BUR = new LabMS.BLL.UserRole();
            LabMS.Model.UserRole MUR = new LabMS.Model.UserRole();
            int UserID = int.Parse(Request.QueryString["UserID"].ToString());
            CheckBox chk;
            bool success = false;
            for (int i = 0; i < gvRole.Rows.Count; i++)
            {
                chk = (CheckBox)gvRole.Rows[i].FindControl("chk");
                if (chk.Checked)
                {
                    int RoleID =int.Parse(gvRole.DataKeys[i].Value.ToString());
                    MUR.RoleID = RoleID;
                    MUR.UserID = UserID;
                    BUR.Add(MUR);
                    success = true;
                }
            }
            if (success == true)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('增加角色信息成功！');window.opener.location.href=window.opener.location.href;self.close();</script>");
            }
            else
            {
                Common.JShelper.JSAlert(this.Page, "", "增加角色信息失败！");
            }
        }
    }
}
